//
//  MeViewModel.m
//  zhuchiren
//
//  Created by rainday on 15/2/1.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "MeViewModel.h"
#import <RACEXTScope.h>
#import <ReactiveCocoa.h>
#import <AFNetworking.h>
#import "IP.h"

@implementation MeViewModel

+ (RACSignal *)setSign:(NSString *)sign{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/update",SERVER];
        NSDictionary *params = @{@"sign":sign}
        ;
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
        }];
        
        
        return nil;
    }];


}

+ (RACSignal *)setPassword:(NSString *)password aNewPassword:(NSString *)aNewPassword{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/update",SERVER];
        NSDictionary *params = @{@"oldpassword":password,
                                 @"newpassword":aNewPassword}
        ;
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
        }];
        
        
        return nil;
    }];

}


+(RACSignal *)setNickname:(NSString *)nickname{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/update",SERVER];
        NSDictionary *params = @{@"nickname":nickname};
        [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
        }];
        
        
        return nil;
    }];
    

}

+(RACSignal *)setAvatar:(UIImage *)avatar{
   
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        id  token = [userDefaults objectForKey:@"token"];
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"Token %@",token] forHTTPHeaderField:@"Authorization"];
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        
        
        NSString *url = [NSString stringWithFormat:@"%s/avatar",SERVER];
        
        [manager POST:url  parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:UIImagePNGRepresentation(avatar)
                                        name:@"avatar"
                                    fileName:@"avatar.png"
                                    mimeType:@"image/png"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
            
        }];

        
        
        return nil;
    }];

}

+ (RACSignal *)getInfoForUser:(NSString *)username{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
       
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/user",SERVER];
        NSDictionary *params = @{@"username":username};
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
        }];
        
        
        return nil;
    }];
    
}


+ (RACSignal *)forgetForPhone:(NSString *)phone password:(NSString *)password{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
        
        [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        
        NSString *url = [NSString stringWithFormat:@"%s/forget",SERVER];
        NSDictionary *params = @{@"phone":phone,
                                 @"password":password};
        [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            [subscriber sendNext:responseObject];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [subscriber sendError:error];
        }];
        
        
        return nil;
    }];

}
@end
